package com.codi.bus.core.dao.impl;

import com.codi.base.dao.BaseDAOImpl;
import com.codi.bus.core.dao.TradeOrderDao;
import com.codi.bus.core.domain.TradeOrder;
import com.codi.bus.core.service.model.FixBankModel;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("tradeOrderDao")
public class TradeOrderDaoImpl extends BaseDAOImpl<TradeOrder> implements TradeOrderDao {

    @Override
    public void add(TradeOrder entity) {
        entity.setCreateDate(new Date());
        entity.setIsValid(true);
        this.insert(entity);
    }

    @Override
    public void updateOrder(TradeOrder entity) {
        entity.setLastUpdatedDate(new Date());
        this.update(entity);
    }

    @Override
    public TradeOrder getEntityByOrderId(Long orderId) {
        return this.getObject(TradeOrder.class.getName() + ".selectByPrimaryKey", orderId);
    }

    @Override
    public List<TradeOrder> getTradeOrderByUserId(Long userId) {
        return getSqlSession().selectList(generateStatement("selectTradeOrderByUserId"), userId);
    }

    @Override
    public void updateStatus(TradeOrder entity) {
        entity.setLastUpdatedDate(new Date());
        this.update(TradeOrder.class.getName() + ".updateStatus", entity);
    }

    @Override
    public List<FixBankModel> selectBankInfoByScheduledProtocolId(List<String> list) {
        Map<String, Object> param = new HashMap<>();
        param.put("list", list);
        return getSqlSession().selectList(generateStatement("selectBankInfoByScheduledProtocolId"), list);
    }

    @Override
    public BigDecimal getSumByDay(TradeOrder model) {
        Object ret = getSqlSession().selectOne(generateStatement("getSumByDay"), model);
        if (ret == null) {
            ret = BigDecimal.ZERO;
        }

        return (BigDecimal) ret;
    }

    @Override
    public BigDecimal getSumByMonth(TradeOrder model) {
        Object ret = getSqlSession().selectOne(generateStatement("getSumByMonth"), model);
        if (ret == null) {
            ret = BigDecimal.ZERO;
        }
        return (BigDecimal) ret;
    }

    @Override
    public List<Date> getConfirmDates(String fundCode, Long userId) {
        Map<String, Object> param = new HashMap<>();

        param.put("fundCode", fundCode);
        param.put("userId", userId);

        return getSqlSession().selectList(generateStatement("getConfirmDates"), param);
    }

    @Override
    public List<Date> getConfirmSellDates(String fundCode, Long userId) {
        Map<String, Object> param = new HashMap<>();

        param.put("fundCode", fundCode);
        param.put("userId", userId);

        return getSqlSession().selectList(generateStatement("getConfirmSellDates"), param);
    }



    @Override
    public Integer getCountBeforeConfirmDate(Long userId, Date ConfirmDate) {
        Map<String, Object> map = new HashMap<>();

        map.put("ConfirmDate", ConfirmDate);
        map.put("userId", userId);
        return getSqlSession().selectOne(generateStatement("getCountBeforeConfirmDate"), map);
    }

    @Override
    public Integer getBuyCountByConfirmDate(Date beginDate, Date endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return getSqlSession().selectOne(generateStatement("getBuyCountByConfirmDate"), map);
    }

    @Override
    public Integer getSellByConfirmDate(Date beginDate, Date endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return getSqlSession().selectOne(generateStatement("getSellByConfirmDate"), map);
    }

    @Override
    public Integer getOffLineByOneWeek(Date beginDate, Long userId, String bankAccount) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginDate", beginDate);
        map.put("userId", userId);
        map.put("bankAccount", bankAccount);

        return getSqlSession().selectOne(generateStatement("getOffLineByOneWeek"), map);
    }

}
